import asyncio
from playwright.async_api import async_playwright

def cookie_string_to_playwright(cookie_string, domain):
    """
    将浏览器复制的 cookie 字符串转换为 Playwright 格式
    :param cookie_string: 例如 "MUSIC_U=xxx; __csrf=yyy; __remember_me=true"
    :param domain: 目标域名（例如 "music.163.com"）
    :return: 列表，每个元素是 Playwright Cookie 结构
    """
    cookies = []
    for pair in cookie_string.split(";"):
        if not pair.strip():
            continue
        name, value = pair.strip().split("=", 1)
        cookies.append({
            "name": name,
            "value": value,
            "domain": domain,
            "path": "/"
        })
    return cookies
url = 'https://music.163.com/playlist?id=4884330070'
cookie_str = '_iuqxldmzr_=32; _ntes_nnid=17174b10f6c22864abea6a7c9cb25dd1,1753138737999; _ntes_nuid=17174b10f6c22864abea6a7c9cb25dd1; NMTID=00O8csmZyUqQQ9kJ0u_kNI6FYEYK0YAAAGYLzY02g; WEVNSM=1.0.0; WNMCID=cmcxoi.1753138738345.01.0; WM_TID=mzNLVVh3L5BARRRQQEeGlF2%2BSj1fP1BY; sDeviceId=YD-lZSVJKlW7OpEBhBRFVaWwB3%2FHmgLp61p; ntes_utid=tid._.e%252F%252Bj2zvPZR5BExUEEFfG0A3qGzkP8rho._.0; __snaker__id=FBFlnt9FJIMDpGs1; ntes_kaola_ad=1; gdxidpyhxdE=T%5CzC55tqlaM3wfUdBW3QHT%2FTs%2BSK7vSYs2imjwjw1Q0WHRBLSjEB39%5COKgox6tLqcToK92722ddzur7Kk8%2FRAA8rGPxa06TQU6iUQ4viUOMtorUqgMEuuvJZcigNOXLGN%2FlAjlRcGApqs9shgknd9pONMTEAjDj8WVQC1I8pLUrTp%5C0W%3A1753234014931; P_INFO=13908700156|1753233233|1|music|00&99|null&null&null#yun&530600#10#0|&0||13908700156; __csrf=396c2ee35178d0bbb3af9e6dce53b9e8; MUSIC_U=00560A34FE9B4D07729CBAC5B3AF1B8FF262D32E772823A58BF87599AC264CB966A32D6844FE5046FA3F3CB29E656E1EAA048412E7BB300F50CC2F9EC20DEAF2243CD0D908CF8C8B8B7A6B9A5646DAFECC0C2E1181DCCC8916C0847CA4F18FEE452DF236DE8817C88F7B0391CD55C3CCBCB20C784799821A621E07BB21AE184D0824F08537DECB2EEBD4C4FFAB8291B904B8E231EF4CECBC428CA957D1CDFFDD7ECAE33A06B8A4CC89B4CC887ED966A5ECB9A90BC0032F8CFEC110F0E14ED1BB11FEE6A9C7FBF85FCB0E4ACEB4C40A870660E5F293464139E52B7DC8A3B751DF87C28AC6CD74A48614ACF9C26541B2BD2734062B800E0D6FF859A090B9C408F58D98D078CD388BC8BBD3A11E41656C93CA9D746CA7C1D85D6E821DC0D7D9E2AC95C45523E88B64689309F019375B251EC7954F629E3C925D28031C476AD20FD80539A5E649A0BE90AC19B4931F94E3D635DEDC96E91322EBE594587D52D814B8B0FB34469FC181551989B073CB067B363A6544827B2E77F37F157902F264853250DA28F46618F95CDC30D59807C4E5B4C4; __remember_me=true; JSESSIONID-WYYY=13RUu55wQxtwKU99KHTTF1EhFD2zeZzyYObYlxt1X8nriqDybHxVq0%2BWuw43ISBkgxAlJZfwrX8ObJ91MT%2FuxftuP2Z5nbn6EIXJJYXBNSbMCqYEgvaTAEEJO1I5eRDlK%2F1MYr9WRCeiJdPO1btDW99NJ8l36CZBfuAl9d99wVbmEAzY%3A1753324619161; WM_NI=bSkUCSC%2BL6Fm5Amy73gZuFxCpLd5%2BSQ33%2B9OvxfgjmJfl2CxIqXFK1bWveICMLyP%2BjGogzKv5YMP40QqOjhH6SRlbCIpSm9HhkTMtBbZlFQG01MSnGkOgLrdq4pmWIsfNjk%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeaccf6b9395a1a8f33ff6b88eb7c45e838e8b86d26886ea96a9e13f949799d9f32af0fea7c3b92aa28da5b6c25981f584d7c567f79fa39ab639a586bba7fb49b494acd6aa5993edfdccbc45b6a8aa88e9678abaa4d3c2748baf98a7ca3ba289ba86cc498f94b7b1aa6285b1a99bca7ef78783b1b73c93af818beb44f68d9e8de1498bb09d8ebc44acb59caef74aa286a4a7d44df4ef83b4ed4da991a9adf170b3ecc08ed643b1ba828bea37e2a3'
print(cookie_string_to_playwright(cookie_str,'music.163.com'))

async def main():
    async with async_playwright() as p:
        brower = await p.chromium.launch(headless=False)
        ctx = await brower.new_context()
        await ctx.add_cookies(cookie_string_to_playwright(cookie_str,'music.163.com'))
        page = await ctx.new_page()
        await page.goto(url)
        #内容没有iframe中的内容
        # print(await page.content())
        frame = page.frame(name="contentFrame")
        content = await frame.content()
        with open('data/music.html','w',encoding='utf-8',newline='') as f:
            f.write(content)
asyncio.run(main())